
package com.noah.project.dao

import com.noah.project.model.Note
import com.noah.project.model.ReplyNote
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository
interface NoteDao {
    @Insert("INSERT INTO note(bid, uid, pic, content, nTime) VALUES(#{bid}, #{uid}, #{pic}, #{content}, #{nTime})")
    @Options(useGeneratedKeys = true, keyProperty = "nid", keyColumn = "nid")//可以返回当前的uid,不加这行返回的是插入的行数
    fun insert(note: Note): Long

    @Delete("DELETE FROM note WHERE nid =#{nid}")
    fun delete(nid: Long?): Long

    @Update("UPDATE note SET content = #{content} WHERE nid = #{nid}")
    fun update(note: Note): Long

    @Select("SELECT * FROM note WHERE nid = #{nid}")
    fun findById(@Param("nid") nid: Long?): Note?

    @Select("SELECT * FROM note WHERE bid = #{bid} AND nTime < #{time} order by nid desc LIMIT 10")
    @Results(
            Result(column = "uid", property = "user", one = One(select = "com.noah.project.dao.UserDao.findReplyById")))
    fun findNotesOfBook(bid: Long?, time: Long?): List<ReplyNote>

    @Select("SELECT * FROM note WHERE nTime < #{time} order by nid desc LIMIT 10")
    @Results(
            Result(column = "uid", property = "user", one = One(select = "com.noah.project.dao.UserDao.findReplyById")),
            Result(column = "bid", property = "book", one = One(select = "com.noah.project.dao.BookDao.findReplyById")))
    fun findNotesOfAll(time: Long?): List<ReplyNote>

    @Select("SELECT * FROM note")
    fun findAll(): List<Note>?
}
